package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //        1.得到前端传递过来的数据
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String username = req.getParameter("username");

        String email = req.getParameter("email");
        String start = req.getParameter("start");
        String end = req.getParameter("end");

        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        String enable = req.getParameter("enable");
        //        2.将数据封装到model
        UserModel userModel = new UserModel();
        userModel.setPage(Tool.pageToInt(page));
        userModel.setLimit(Tool.limitToInt(limit));

        userModel.setUsername(Tool.nullToString(username));

        userModel.setSex(Tool.nullToString(sex));
        userModel.setStart(Tool.nullToString(start));
        userModel.setEnd(Tool.nullToString(end));
        userModel.setEnable(Tool.stringToInt(enable));
        userModel.setEmail(Tool.nullToString(email));
        userModel.setHobby(Tool.nullToString(hobby));
        userModel.setPhone(Tool.nullToString(phone));

        //        3.创建服务逻辑层
        UserService userService = new UserServiceImpl();
        //        4.接收服务逻辑层返回信息

        ResultModel resultModel = userService.findAll(userModel);
        //        5.将信息返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));
    }
}
